package com.me.mygdxgame;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.me.ultilities.Constants;
import com.me.ultilities.DrawComponent;
import com.me.ultilities.LoadData;

public class MainGameArea implements Screen {
	private SpriteBatch spriteBatch;
	private Texture texture;
	private Texture texture1;
	private Texture texture2;
	private DrawComponent draw;
	private float index = 0;

	private float stateTime = 0f;
	private Stage stage;
	private Animation animation;
	private Image image;
	private PigAnimation pig;
	@Override
	public void render(float delta) {
		// TODO Auto-generated method stub

		if (index == 270)
			index = 0;
		Gdx.gl.glClearColor(0, 0, 0, 1);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		spriteBatch.begin();
		draw.drawPicture(texture, spriteBatch, 0, 0, 480, 800);
		draw.drawPicture(texture2, spriteBatch, 340, 80, 124, 110);
		
		
		stateTime += Gdx.graphics.getDeltaTime();
		if (index <= 270 && pig.isLocked()==false) {			
			pig.start(draw, spriteBatch, animation, stateTime, index);		
		}	
		
		spriteBatch.end();
		stage.act(delta);
		stage.draw();

		if (index != 270)
			index += 1f;
		
		
	}

	@Override
	public void resize(int width, int height) {
		// TODO Auto-generated method stub

	}

	@Override
	public void show() {
		// TODO Auto-generated method stub
		Gdx.input.setInputProcessor(new InputAdapter() {

			@Override
			public boolean touchDown(int screenX, int screenY, int pointer,
					int button) {
				 float finalX = screenX - 17;
				 float finalY = 480 - screenY - 17;
				// TODO Auto-generated method stub
			if(Math.abs(finalX - pig.getX())<=20 && Math.abs(finalY-pig.getY())<=20) {	
				pig.setX(finalX);
				pig.setY(finalY);
				pig.setStatus(2);
			}
			else System.out.println("non - touched ");
				
				// ---------------------> click mouse here			
				image.clearActions();
				image.setPosition(finalX,finalY );
				image.setScale(1);
				image.addAction(Actions.scaleTo(0, 0, 0.25f));
				
				return super.touchDown(screenX, screenY, pointer, button);
			}

		});
		pig = new PigAnimation();
		draw = new DrawComponent();
		stage = new Stage(800, 480, true);
		spriteBatch = new SpriteBatch();
		texture = LoadData.getTexture(Constants.BACKGROUND);
		texture1 = LoadData.getTexture(Constants.BALL);
		texture2 = LoadData.getTexture(Constants.VASE_LV1);

		image = new Image(texture1);
		image.setSize(40, 40);
		image.setOrigin(20, 20);
		stage.addActor(image);
		image.setScale(0);
	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub

	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

}
